﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;
using System.Diagnostics;



namespace FAST
{
    public partial class VersioningUI : Form
    {
        private FileHandler fh = new FileHandler();
        private List<FileHandler> fVL;
        private MainUI main;
        
        public VersioningUI()
        {
            InitializeComponent();
        }

        public VersioningUI(MainUI main, FileHandler versionObj, List<FileHandler> versionList)
        {
            InitializeComponent();

            this.MouseHover += new EventHandler(this.form_MouseHover);
            this.hoverPanel.MouseHover += new EventHandler(this.hoverPanel_MouseHover);
            this.dgdVersion.MouseHover += new EventHandler(this.form_MouseHover);

            this.main = main;
            this.fh = versionObj;
            this.fVL = versionList;

            FileInfo fi = fh.getFi();

            lblFilename.Text = "Filename: "+fi.Name;
            lblFilepath.Text = "Filepath: "+fi.FullName;

            dgdVersion.DataSource = this.createVersionList();
            this.FormClosed += new FormClosedEventHandler(this.versionUI_Close);
            this.versionRightClickMenu.ItemClicked += new System.Windows.Forms.ToolStripItemClickedEventHandler(this.versionRightClickMenu_ItemClicked);
        }

        public void form_MouseHover(object sender, EventArgs e)
        {
            this.FormBorderStyle = FormBorderStyle.None;
        }

        public void hoverPanel_MouseHover(object sender, EventArgs e)
        {
            this.FormBorderStyle = FormBorderStyle.Sizable;
        }

        public void versionRightClickMenu_ItemClicked(Object sender, EventArgs e)
        {
            try
            {
                int index = int.Parse(versionRightClickMenu.Items[versionRightClickMenu.Items.Count - 1].Text);
                if (versionRightClickMenu.Items[0].Selected)
                {
                    fh = fVL[index];                   
                    string path = fh.getFullFilePath();
                    FileInfo fi = new FileInfo(path);
                    Process.Start(path);
                }
                else if (versionRightClickMenu.Items[1].Selected)
                {
                    fh = fVL[index];
                    //Copy to
                    String fileToCopy = fh.fullFilePath;
                    String fileName = fileToCopy.Substring(fileToCopy.LastIndexOf('\\') + 1);
                    //Open Folder Browser
                    String desFolder = "";

                    if (folderBrowser.ShowDialog() == DialogResult.OK)
                    {
                        desFolder = folderBrowser.SelectedPath;
                        File.Copy(fileToCopy, (desFolder + @"\" + fileName), true);
                    }
                }
            }
            catch (Exception e2)
            {
                Console.WriteLine("" + e2);
            }
        }

        public ArrayList createVersionList()
        {
            ArrayList tempVersionList = new ArrayList();
            VersioningUIObject vo;
            FileInfo fi;
            int vSize = fVL.Count;

                for (int i = 0; i < vSize; i++)
                {
                    fi = fVL[i].getFi();
                    vo = new VersioningUIObject(fi.Name.ToString(), fi.LastWriteTime.ToString(), fVL[i].getComments());
                    tempVersionList.Add(vo);
                }

            return tempVersionList;
        }

        public void versionUI_Close(Object sender, EventArgs e)
        {
            switch (MessageBox.Show("Do you wish to save the comments?",
                                           "Warning",
                                          MessageBoxButtons.YesNo,
                                          MessageBoxIcon.Exclamation))
            {
                case DialogResult.Yes:
                    this.btnOK_Click(sender,e);
                    break;
                case DialogResult.No:
                    this.Dispose();
                    break;
            }                     
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            int vSize = fVL.Count;
            for (int i = 0; i < vSize; i++)
            {
                fVL[i].setComments(dgdVersion.Rows[i].Cells["iComment"].EditedFormattedValue.ToString());
            }

            main.setVersionComments(fVL, this.fh.getVersioningIdx());//save comments
            main.btnPreview_Click(main, new EventArgs());
            this.Dispose();
        }

        private void dgdVersion_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex >= 0 && e.ColumnIndex >= 0 && e.Button == MouseButtons.Right)
            {
                dgdVersion.Rows[e.RowIndex].Selected = true;
                Rectangle r = dgdVersion.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true);
                versionRightClickMenu.Show((Control)sender, r.Left + e.X, r.Top + e.Y);

                int totalCount = versionRightClickMenu.Items.Count;
                versionRightClickMenu.Items[totalCount - 1].Text = e.RowIndex.ToString();

            }
        }
    }
}
